package com.ruoyi.user.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.user.domain.TripUser;
import com.ruoyi.user.service.ITripUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 乘客管理Controller
 *
 * @author trip
 * @date 2024-09-10
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/user/user")
public class TripUserController extends BaseController {

    private final ITripUserService tripUserService;

/**
 * 查询乘客管理列表
 */
@PreAuthorize("@ss.hasPermi('user:user:list')")
@GetMapping("/list")
    public TableDataInfo list(TripUser tripUser) {
        startPage();
        LambdaQueryWrapper<TripUser> lqw = new LambdaQueryWrapper<TripUser>();
                        if (StringUtils.isNotBlank(tripUser.getUsername())){
                        lqw.like(TripUser::getUsername ,tripUser.getUsername());
                    }
                        if (StringUtils.isNotBlank(tripUser.getPassword())){
                        lqw.eq(TripUser::getPassword ,tripUser.getPassword());
                    }
                        if (StringUtils.isNotBlank(tripUser.getNickName())){
                        lqw.like(TripUser::getNickName ,tripUser.getNickName());
                    }
                        if (StringUtils.isNotBlank(tripUser.getPhone())){
                        lqw.eq(TripUser::getPhone ,tripUser.getPhone());
                    }
                        if (StringUtils.isNotBlank(tripUser.getEmail())){
                        lqw.eq(TripUser::getEmail ,tripUser.getEmail());
                    }
                        if (StringUtils.isNotBlank(tripUser.getAvatar())){
                        lqw.eq(TripUser::getAvatar ,tripUser.getAvatar());
                    }
                        if (tripUser.getSex() != null){
                        lqw.eq(TripUser::getSex ,tripUser.getSex());
                    }
                        if (StringUtils.isNotBlank(tripUser.getEmergencyPerson())){
                        lqw.eq(TripUser::getEmergencyPerson ,tripUser.getEmergencyPerson());
                    }
                        if (StringUtils.isNotBlank(tripUser.getEmergencyPhone())){
                        lqw.eq(TripUser::getEmergencyPhone ,tripUser.getEmergencyPhone());
                    }
                        if (tripUser.getDelFlag() != null){
                        lqw.eq(TripUser::getDelFlag ,tripUser.getDelFlag());
                    }
                        if (tripUser.getStatus() != null){
                        lqw.eq(TripUser::getStatus ,tripUser.getStatus());
                    }
        List<TripUser> list = tripUserService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TripUser> util = new ExcelUtil<TripUser>(TripUser.class);
        util.importTemplateExcel(response, "乘客管理数据");
    }

    /**
     * 导入乘客管理数据
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('user:user:import')")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TripUser> util = new ExcelUtil<TripUser>(TripUser.class);
        List<TripUser> list = util.importExcel(file.getInputStream());

        if (list.size() <= 0) {
            throw new RuntimeException("导入数据为空,请填充数据再导入！");
        }
        // 批量存入
            tripUserService.saveBatch(list);

        return AjaxResult.success("导入成功");
    }

    /**
     * 导出乘客管理列表
     */
    @PreAuthorize("@ss.hasPermi('user:user:export')")
    @Log(title = "乘客管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TripUser tripUser) {
        List<TripUser> list = tripUserService.selectTripUserList(tripUser);
        ExcelUtil<TripUser> util = new ExcelUtil<TripUser>(TripUser.class);
        util.exportExcel(response, list, "乘客管理数据");
    }

    /**
     * 获取乘客管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return AjaxResult.success(tripUserService.selectTripUserByUserId(userId));
    }

    /**
     * 新增乘客管理
     */
    @PreAuthorize("@ss.hasPermi('user:user:add')")
    @Log(title = "乘客管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TripUser tripUser) {
        return toAjax(tripUserService.insertTripUser(tripUser));
    }

    /**
     * 修改乘客管理
     */
    @PreAuthorize("@ss.hasPermi('user:user:edit')")
    @Log(title = "乘客管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TripUser tripUser) {
        return toAjax(tripUserService.updateTripUser(tripUser));
    }

    /**
     * 删除乘客管理
     */
    @PreAuthorize("@ss.hasPermi('user:user:remove')")
    @Log(title = "乘客管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(tripUserService.deleteTripUserByUserIds(userIds));
    }
}


